from enum import Enum


def DictEnum(key, name):
    def decorator(cls):
        cls.dict_key = key
        cls.dict_name = name
        return cls

    return decorator


class CodedEnum(Enum):
    """
    字典基类
    """

    def __new__(cls, code, msg):
        obj = object.__new__(cls)
        obj.code = code
        obj.msg = msg
        return obj


def parse_dict_enum(enum_class):
    """
    解析字典枚举类
    """
    items = []
    for member in enum_class:
        items.append(
            {
                "name": member.msg,
                "dictItemValue": member.code,
                "dictItemKey": member.code,
            }
        )
    return {
        "dictKey": getattr(enum_class, "dict_key", None),
        "name": getattr(enum_class, "dict_name", None),
        "items": items,
    }
